package com.cloud.bc.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.cloud.bc.config.Config;
import com.cloud.bc.model.enums.CookieConst;
import com.cloud.common.util.CookieUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@AllArgsConstructor
@Service
public class BcUtils {

    private final Config config;

    /**
     * 获取图片地址
     */
    public String getListImgUrl(String imgUrl) {
        if (imgUrl.contains("http://") || imgUrl.contains("https://")) {
            return imgUrl;
        }
        return config.getImgUrl() + imgUrl + "_200x140.jpg";
    }

    /**
     * 获取第一张图片
     */
    public String getFirstImgUrl(String imgUrl) {
        if (StrUtil.isEmpty(imgUrl)) {
            return "";
        }
        if (StrUtil.contains(imgUrl, ",")) {
            imgUrl = StrUtil.subBefore(imgUrl, ",", false);
        }
        return getListImgUrl(imgUrl);
    }

    //获取OSS上传路径
    public String createOssFiledir(String fileType, String staff) {
        return fileType + "/" + DateUtil.today().replace("-", "/") + "/" + UUID.randomUUID().toString() + "." + staff;
    }

    /**
     * 获取推荐二维码路径
     */
    public String createOssFiledir(String fileType, String staff, String eid) {
        return fileType + "/" + eid + "." + staff;
    }

    /**
     * 判断跳转到m还pc端
     */
    public String gotoUrl(HttpServletRequest request, String url) {
        String cookie = CookieUtils.getCookie(request, CookieConst.platformCookie);
        if (!StrUtil.isEmpty(cookie) && cookie.equals("m")) {
            url = "/m" + url;
        }
        return url;
    }

    /**
     * 判断跳转到m还pc端
     */
    public String gotoView(HttpServletRequest request, String url) {
        String cookie = CookieUtils.getCookie(request, CookieConst.platformCookie);
        if (!StrUtil.isEmpty(cookie) && cookie.equals("m")) {
            url = "m/" + url;
        } else {
            url = "pc/" + url;
        }
        return url;
    }

    /**
     * 判断跳转到m还pc端,默认PC认为是空目录
     */
    public String gotoViewDefaul(HttpServletRequest request, String url) {
        String cookie = CookieUtils.getCookie(request, CookieConst.platformCookie);
        if (!StrUtil.isEmpty(cookie) && cookie.equals("m")) {
            return "m/" + url;
        }
        return url;
    }
}
